package com.example.controller;

import com.example.model.CargoRequest;
import com.example.model.DeckRequest;
import com.example.model.Result;
import com.example.service.impl.CargoInterServiceImpl;
import com.example.service.impl.DeckInterServiceImpl;
import com.example.service.impl.LoadSessionServiceimpl;
import com.example.service.LoadingService;
import com.example.utils.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/smart-ship")
public class InterController {

    @Autowired
    private LoadSessionServiceimpl sessionService;    // 会话管理服务

    @Autowired
    private CargoInterServiceImpl cargoService;

    @Autowired
    private DeckInterServiceImpl deckService;

    // —— 1. 开启新会话 ——
    @PostMapping("/start")
    public Result startSession(
//            @RequestHeader("timestamp") String timestamp,
//            @RequestHeader("sign") String sign
    ) throws NoSuchAlgorithmException {
//        //  验签名
//        String expected = SignUtils.generateSign(timestamp);
//        if (!expected.equals(sign)) {
//            return Result.error("没有授权");
//        }
        //  创建会话并返回 sessionId
        return Result.sessionsuccess(sessionService.createSession());
    }

    // —— 2. 上传货物 ——
    // 带 sessionId 的路径参数
    @PostMapping("/{sessionId}/cargo")
    public Result handleCargo(
            @PathVariable String sessionId,
            @RequestBody List<CargoRequest> cargoRequestList,
            @RequestHeader("timestamp") String timestamp,
            @RequestHeader("sign") String sign
    ) throws NoSuchAlgorithmException {
        // 验签
        String expected = SignUtils.generateSign(timestamp);
        if (!expected.equals(sign)) {
            return Result.error("没有授权");
        }
        //验证货物数据
//        if (!cargoService.verifyCargoData(cargoRequestList)) {
//            return Result.error("请求参数有误，请检查");
//        }
        //处理货物数据
        cargoService.proCargoData(cargoRequestList);
        log.info("[{}] 接受货物: {}", sessionId, cargoRequestList);
        // 委托 SessionService 校验 & 缓存
        return sessionService.saveCargo(sessionId, cargoRequestList);
    }

    // —— 3. 上传甲板并触发装载 ——
    @PostMapping("/{sessionId}/deck")
    public Result handleDeck(
            @PathVariable String sessionId,
            @RequestBody DeckRequest deckRequest,    // 甲板 DTO :contentReference[oaicite:6]{index=6}&#8203;:contentReference[oaicite:7]{index=7}
            @RequestHeader("timestamp") String timestamp,
            @RequestHeader("sign") String sign
    ) throws NoSuchAlgorithmException {
        // 验签
        String expected = SignUtils.generateSign(timestamp);
        if (!expected.equals(sign)) {
            return Result.error("没有授权");
        }
        // 验证甲板数据
//        if (!deckService.verifyDeckData(deckRequest)) {
//            return Result.error("请求参数有误，请检查");
//        }
        log.info("[{}] 接受甲板: {}", sessionId, deckRequest);

        // 委托 SessionService 校验、触发排载、清理会话
        // 最终会返回 ReponseResult（包含货物坐标等）
        return Result.success(sessionService.saveDeckAndLoad(sessionId, deckRequest))   ;
    }
}
